package iterator;

import java.util.Arrays;

/**
 * 实现的存储数据集合类
 **/
public class MyCollection<T> implements ICollection<T> {

	private T[] arys;
	private int index    = -1;
	private int capacity = 6;

	@SuppressWarnings("unchecked")
	public MyCollection() {

		this.arys = (T[]) new Object[capacity];
	}

	@Override
	public IIterator<T> iterator() {

		return new MyIterator<>(this);
	}

	@Override
	public void add(T t) {

		index++;
		if (index == capacity) {
			capacity += 6;
			this.arys = Arrays.copyOf(arys, capacity);
		}
		this.arys[index] = t;
	}

	@Override
	public T get(int index) {

		if (index < size()) {
			return this.arys[index];
		}

		return null;
	}

	@Override
	public int size() {

		return index + 1;
	}

}
